package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import server.BkStfITF;
import server.PersonInterface;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import client.Configuration;

public class BkStaffLoginGUI extends JFrame {

	private JPanel contentPane;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	
	Configuration config=new Configuration();
	String rmiAddress=config.getRmiAddress();
	BkStfITF Sitf;
    Person ps = new Person();
    int pID = ps.getPID();
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BkStaffLoginGUI frame = new BkStaffLoginGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BkStaffLoginGUI() {
		setTitle("\u804C\u5DE5\u767B\u5F55");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("请输入您的登录密码：");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label.setBounds(31, 34, 221, 27);
		contentPane.add(label);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(114, 108, 174, 36);
		contentPane.add(passwordField);
		
		JButton button = new JButton("\u767B\u5F55");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Sitf = (BkStfITF) Naming.lookup(rmiAddress+"BankStaff");
				    String skey = passwordField.getText().toString();
				
					if(skey.equals(""))
						JOptionPane.showMessageDialog(null, "输入不可为空！");
					else{
						int pkey = Integer.valueOf(skey).intValue();//Integer.parseInt(skey);
						if(Sitf.checkKey(pID, pkey)==false)
							JOptionPane.showMessageDialog(null, "身份证或密码错误！");
						else if(pID==1)
						{
							BkManagerWorkGUI frame = new BkManagerWorkGUI();
							frame.setLocationRelativeTo(null);
						    frame.setVisible(true);
						    BkStaffLoginGUI.this.dispose();
					    }else{
							BkStaffWorkGUI frame = new BkStaffWorkGUI();
							frame.setLocationRelativeTo(null);
							frame.setVisible(true);
						    BkStaffLoginGUI.this.dispose();
					    }
					}
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}
		});
		button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button.setBounds(312, 107, 74, 45);
		contentPane.add(button);
		
		JButton button_1 = new JButton("\u52A0\u5165\u6211\u4EEC..");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

					/*Sitf = (BkStfITF) Naming.lookup(rmiAddress+"BankStaff");
					String skey = passwordField.getText();
					
					if(skey.equals(""))
						JOptionPane.showMessageDialog(null, "输入不能为空");
					else{
						int pID = ps.getPID();
						int pkey = Integer.parseInt(skey);
						if(Sitf.checkKey(pID, pkey)==false)
						JOptionPane.showMessageDialog(null, "已登记，感谢参与！有聘用信息我们会及时与您联络。");
					else{
						BkStaffRegisterGUI frame = new BkStaffRegisterGUI();
						frame.setVisible(true);
						BkStaffLoginGUI.this.dispose();
					    }
						}*/
					dispose();
					BkStaffRegisterGUI frame = new BkStaffRegisterGUI();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
					
				
				
			}
		});
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_1.setBounds(291, 213, 119, 27);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("\u8FD4\u56DE");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				HomePageGUI frame = new HomePageGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		button_2.setBounds(341, 10, 83, 22);
		contentPane.add(button_2);
	}

}
